package gms.service.field;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import gms.dao.field.FieldDao;
import gms.entities.field.Field;
import gms.entities.field.Field_rental;
import gms.entities.field.Rent;

@Transactional
public class FieldService {

	//�� spring ע��
	private FieldDao fieldDao;
	private Rent rent = new Rent();
	public void setFieldDao(FieldDao fieldDao) {
		this.fieldDao = fieldDao;
	}

		//����ID���ҳ��أ�����һ�����ض���
		public Field findFieldById(int id){
			return fieldDao.findFieldById(id);
		}
		
		//�����͹ؼ��ֵĲ���
		public List<Field> searchByType(String fieldType){
			List<Field> list = fieldDao.searchByType(fieldType);
			if(list.size() != 0){
				//�м�¼�򷵻�
				
				return list;
			}else{
				//�޼�¼����null action�����ж� 
				return null;
			}
		}
		
		
		//查找所有的场地信息
		public List<Field> findAllField(){
			List<Field> list = fieldDao.findAllField();
			int num = list.size();
			return list;
		}
		//查找用户租用的和未被租用的场地
		public List<Field> findUserField(String username){
			List<Field> list = fieldDao.findUserField(username);
			int num = list.size();
			return list;
		}
		
		//查找所有的场地信息
		public List<Field_rental> findAllFieldRental(){
			List<Field_rental> list = fieldDao.findAllFieldRental();
			//租金表记录总数
			int num = list.size();
			return list;
		}
		
		//action ��һ�� ������� ���и���
		public void updateField(Field f){
			System.out.println(f.getFacilityId());
			f = save_field_rental(f);
			fieldDao.updateField(f);
		}
		
		//���泡����Ϣ
		public void saveField(Field f){
			f = save_field_rental(f);
			fieldDao.saveField(f);
		}
		
		//ɾ��һ����¼
		public void deleteField(Field f){
			Field new_f = fieldDao.findFieldById(f.getFacilityId());
			fieldDao.deleteField(new_f);
		}
		
		//����ĸ�������  ����һ��������
		public Field save_field_rental(Field f){
			String type = f.getField_type();
			int var = 0;
			
			switch(type){
				case "篮球场":
					var=1401;
					break;
				case "足球场":
					var=1402;
					break;
				case "羽毛球场":
					var=1403;
					break;
				case "棒球场":
					var=1404;
					break;
			}
			
			List<Field_rental> fr_list = fieldDao.rentalRecordById(var);
			Field_rental fr = fr_list.get(0);
			f.setField_rental(fr);
			return f;
		}
		public String rent(int i,String username){
			System.out.println(i);
			Field f = fieldDao.findFieldById(i);
			//当当前的标记 == 0 时
			if(f.getField_mark() == 0){
				//保存订单信息
				//设置当前用户名
				rent.setUsername(username);
				rent.setFacilityId(f.getFacilityId());
				rent.setPayment_amount(f.getField_rental().getRental_amount());
				Date dt=new Date();//如果不需要格式,可直接用dt,dt就是当前系统时间
				rent.setRent_time(dt);
				saveRent(rent);
				//设置租用标记
				f.setField_mark(1);
				fieldDao.updateField(f);
				return "true";
			}else{
				List<Rent> myRentList = findRentByUsername(username);
				if(myRentList.size() != 0){
					for(Rent r : myRentList){
						//判断用户是否一致
						if(r.getFacilityId() == f.getFacilityId()){
							deleteRentById(f.getFacilityId());
							//取消租用标记
							f.setField_mark(0);
							fieldDao.updateField(f);
							return "false";
						}
					}
				}
			}
			return "none";
			
		}
		
		//根据类型查找没有被租用的场地id
		public String findFieldByTypeAndNoRent(String field_type) {
			// TODO Auto-generated method stub
			String str = "";
			List<Field> list = searchByType(field_type);
			if(list.size() != 0){
				for(int i=0;i<list.size();i++){
					if(list.get(i).getField_mark() == 0){
						str += list.get(i).getFacilityId() + ",";
					}
				}
				return str;
			}else{
				return null;
			}


		}
		
		//保存rent 对象  存在数据库中的一张表
		public void saveRent(Rent rent){
			fieldDao.saveRent(rent);
		}
		
		public void deleteRentById(int facillityId){
			fieldDao.deleteRentById(facillityId);
		}
		//用户名查找rent
		public List<Rent> findRentByUsername(String username){
			List<Rent> list = fieldDao.findRentByUsername(username);
			return list;
		}
		

		//修改租金表
		public List<Field_rental> modifyRental() {
			// TODO Auto-generated method stub
			List<Field_rental> list = findAllFieldRental();
			return list;
		}
		//更新租金表
		public void updateRental(Field_rental fr){
			fieldDao.updateFieldRental(fr);
		}
		
		
	
}
